
require("config")
require("framework.init")
require("framework.shortcodes")
require("framework.cc.init")

local GameScene  = class("GameScene", function()
    return display.newScene("GameScene")
end)


local TAG_SPRITENODE       = 10

function GameScene:ctor()
  --local backColor = display.newColorLayer(ccc4(20,5,40,128)):addTo(self)
  ---[[
  local item = ui.newImageMenuItem({image='CloseNormal.png',imageSelected='CloseSelected.png',listener=onClicked,tag=1})
  local menu = ui.newMenu({item}):addTo(self)
  menu:pos(display.width-40,display.height-40)
  menu:setZOrder(2)
  --]]
  --self:addNodeEventListener(cc.NODE_ENTER_FRAME_EVENT, function (dt) self:onEnterFrame(dt) end)       --帧定时器，用于删除body,sprite
  --self:scheduleUpdate()
  ---[[
  local touch = display.newLayer():addTo(self)
  touch:setTouchEnabled(true)
  touch:addNodeEventListener(cc.NODE_TOUCH_EVENT, onTouch)
  --]]
  --[[
  local sprite = CCSprite:create('andy.png', CCRect(0, 0, 192, 192)):addTo(self)
  sprite:pos(display.cx, display.cy+200)
  local sprite2 = CCSprite:create('andy.png', CCRect(192, 0, 192, 192)):addTo(self)
  sprite2:pos(display.cx, display.cy+100)
  ]]
  ---[[
  fArray = CCArray:create()
  texture = CCTextureCache:sharedTextureCache():addImage('andy.png')
  for i=0,4 do
    for j=0,16 do
      --local sp = CCSpriteFrame:create('andy.png',CCRect(i*192, j*192, 192, 192))
      local sp = CCSpriteFrame:createWithTexture(texture,CCRect(i*192, j*192, 192, 192))
      fArray:addObject(sp)
      CCSpriteFrameCache:sharedSpriteFrameCache():addSpriteFrame(sp, 'sp['.. i ..'][' .. j ..']')
    end
  end
  --]]
---[[

  --local spriteNode = display.newNode():addTo(self)
  local spriteNode = display.newLayer():addTo(self)
  spriteNode:setTag(TAG_SPRITENODE)
  sprite = CCSprite:createWithSpriteFrame(fArray:objectAtIndex(0)):addTo(spriteNode)
  sprite:pos(display.cx, display.cy)
  anim = CCAnimation:createWithSpriteFrames(fArray,0.2)
  transition.playAnimationForever(sprite,anim)

  fArray2 = fArray:copy()
  fArray2:reverseObjects()
  anim2 = CCAnimation:createWithSpriteFrames(fArray2,0.2)
  sprite2 = CCSprite:create():addTo(spriteNode)
  sprite2:pos(display.cx, display.cy-200)
  transition.playAnimationForever(sprite2,anim2)
  --]]

  printf('FPS=%g',CCDirector:sharedDirector():getAnimationInterval())

end

--local bStop = false
local bRuning = false
function onClicked(tag)
  CCLuaLog('... onClicked ...')
  --sprite:stopAllActions() 
  --sprite2:stopAllActions() 

  --[[
  if not bStop then
    CCDirector:sharedDirector():getActionManager():pauseTarget(sprite)
    CCDirector:sharedDirector():getActionManager():pauseTarget(sprite2)
    bStop = true
 else
    CCDirector:sharedDirector():getActionManager():resumeTarget(sprite)
    CCDirector:sharedDirector():getActionManager():resumeTarget(sprite2)
    bStop = false
  end
  ]]

  local x,y = math.random(20,50),math.random(100,200)
  local d = math.random(0,1)
  if d == 0 then x = -x end
  d = math.random(0,1)
  if d == 0 then y = -y end

  if not bRuning then
    bRuning = true
    local myCallFun = CCCallFunc:create(function() bRuning = false end)
    local myMove = CCMoveBy:create(0.5, CCPoint(x,y))
    local mySeq = CCSequence:createWithTwoActions(myMove, myCallFun)
    d = math.random(0,1)
    if d == 0 then
      sprite:runAction(mySeq)
    else
      sprite2:runAction(mySeq)
    end
  end
  return false
end


function onTouch(event)
  if event.name == 'began' then
    CCLuaLog('... onTouch ...')
  elseif event.name == 'moved' then
    local dx,dy = event.x-event.prevX,event.y-event.prevY
    local scene = display.getRunningScene():getChildByTag(TAG_SPRITENODE)
    local x,y = scene:getPositionX()+dx,scene:getPositionY()+dy
    scene:pos(x,y)
  end

 
  return true
 
end


function GameScene:onEnter()
	CCLuaLog('GameScene:onEnter')																				--启用定时器监听
end


function GameScene:enterScene(sceneName, args, transitionType, time, more)
	 print('GameScene:enterScene..................')
end

function GameScene:onEnterFrame(dt)
  
end

game = {}

function game.startup()
    CCFileUtils:sharedFileUtils():addSearchPath("res/")
    print('startup..................')
    game.enterMainScene()
end

function game.exit()
    CCDirector:sharedDirector():endToLua()
    print('exit..........')
end

function game.enterMainScene()
	print('GameScene.new',type(GameScene.new))
    display.replaceScene(GameScene.new(), "fade", 0.6, display.COLOR_WHITE)
end
return game